package com.panopset.gui;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

/**
 * Full screen window.  Esc key always disposes it.
 * Usually you would add an animated panel, such as a com.panopset.gui.Osgw panel
 * to the public final frame object.
 *
 * Override show and quit methods to restore and save state, as needed.
 *
 * @author Karl Dinwiddie
 *
 */
public class FullScreenWindow {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    public final JFrame frame;

    protected void reportKeyPressed(int keyCode) {

    }

    protected void reportMouseClicked(Point p) {

    }

    protected void reportMouseMoved(Point p) {

    }

    protected void reportMouseDragged(Point p) {

    }

    public FullScreenWindow() {
        frame = new JFrame();
        frame.setUndecorated(true);
        frame.setAlwaysOnTop(true);
        frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
        frame.setLocation(0, 0);
        frame.addMouseMotionListener(new MouseMotionListener() {

            @Override
            public void mouseDragged(MouseEvent e) {
                reportMouseDragged(e.getPoint());
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                reportMouseMoved(e.getPoint());
            }

        });
        frame.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                reportMouseClicked(e.getPoint());
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // nothing to do here.
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // nothing to do here.
            }

            @Override
            public void mousePressed(MouseEvent e) {
                // nothing to do here.
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                // nothing to do here.
            }

        });
        frame.addKeyListener(new KeyListener() {

            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    quit();
                } else {
                    reportKeyPressed(e.getKeyCode());
                }
            }

            public void keyReleased(KeyEvent e) {
                // nothing to do here.
            }

            public void keyTyped(KeyEvent e) {
                // nothing to do here.
            }

        });
    }

    public void show() {
        frame.setVisible(true);
        gd.setFullScreenWindow(frame);
    }

    public void quit() {
        Osgw.close();
        gd.setFullScreenWindow(null);
        frame.dispose();
    }
}
